package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.cartoonutil.lib.BaseController;
import com.stark.cartoonutil.lib.CartoonsController;
import com.stark.cartoonutil.lib.utils.ImgUtil;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPicAnimationBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;

public class PicAnimationActivity extends BaseAc<ActivityPicAnimationBinding> {

    public static String imgPath;
    private CartoonsController cartoonsController;
    private Bitmap bitmap;
    private int defWidth = -1;
    private int defHeight = -1;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_animation;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPicAnimationBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvReselect.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvReselect://重选图片
                Intent intent = new Intent(this, SelPicActivity.class);
                intent.putExtra(Content.Again, "Again");
                startActivityForResult(intent, Content.PICTURE_REPLACE);
                break;
            case R.id.tvSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        //该控制器用于对图片进行动漫化处理
        cartoonsController = new CartoonsController(mContext, mDataBinding.glImgView);
        showDialog(getString(R.string.loading));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (defWidth < 0) {
                    defWidth = mDataBinding.glImgView.getWidth();
                    defHeight = mDataBinding.glImgView.getHeight();
                }
                bitmap = ImgUtil.Companion.getCompressedBitmapByFilePath(imgPath, defWidth, defHeight);
                ViewGroup.LayoutParams layoutParams = mDataBinding.glImgView.getLayoutParams();
                layoutParams.width = bitmap.getWidth();
                layoutParams.height = bitmap.getHeight();
                mDataBinding.glImgView.setLayoutParams(layoutParams);
                mDataBinding.glImgView.setImageSrc(bitmap.copy(bitmap.getConfig(), bitmap.isMutable()), true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        cartoonsController.applyCartoonsModel(new BaseController.IApplyModelCallback() {
                            @Override
                            public void onComplete() {
                                dismissDialog();
                            }
                        });
                    }
                }, 500);
            }
        }, 500);
    }

    private void saveImg() {
        showDialog(getString(R.string.saving));
        float aspectRatio = mDataBinding.glImgView.getWidth() * 1f / mDataBinding.glImgView.getHeight();
        cartoonsController.save(aspectRatio, new BaseController.ISaveCallback() {
            @Override
            public void onResult(@Nullable Uri fileUri) {
                dismissDialog();
                if (fileUri != null) {
                    ToastUtils.showShort(fileUri != null ? getString(R.string.save_success) : getString(R.string.save_failure));
                    startActivity(HomeActivity.class);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //这里要释放资源
        if (cartoonsController != null) {
            cartoonsController.release();
            cartoonsController = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String path = data.getStringExtra(Content.imgPath);
            if (requestCode == Content.PICTURE_REPLACE) {
                showDialog(getString(R.string.loading));
                imgPath = path;
                bitmap = ImgUtil.Companion.getCompressedBitmapByFilePath(imgPath, defWidth, defHeight);
                mDataBinding.glImgView.setImageSrc(bitmap.copy(bitmap.getConfig(), bitmap.isMutable()), true);
                cartoonsController.applyCartoonsModel(new BaseController.IApplyModelCallback() {
                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }
                });
            }
        }
    }

}